<script setup>

// 使用假数据，真实环境需要去掉
import '../mock/mymock.js'
// API
import approveApi from '../api/approve.js'
import userApi from '../api/user.js'

const userStore = {};

const list = ref([])

function getList(userId) {

  approveApi.getList(userId).then(res=>{
    list.value = res;
  });
}

// 通过审批
function approve(item) {
  $.ajax({
    url: reqUrl+'/system/approve/sendFlow',
    data:{
      flowType:0,
      autoId:item.autoId,
      cursTepId:item.stepId,
      autoexecute:item.autoexecute,
      userNameId:item.flowAutoId
    },
    type: 'POST',
    success: function (res) {
      if(res.code === '200'){
        return window.location.href= reqUrl+'/system/approve/index3';
      }else{
        let msg = res.message+":"+res.remark.message;
        console.log(msg);
        alert(msg);
      }
    }
  });
}

function reject(item) {
  $.ajax({
    url: reqUrl+'/system/approve/sendFlow',
    data:{
      flowType:1,
      autoId:item.autoId,
      cursTepId:item.stepId,
      autoexecute:item.autoexecute,
      userNameId:item.flowAutoId
    },
    type: 'POST',
    success: function (res) {
      if(res.code === '200'){
        window.location.href= reqUrl+'/system/approve/index3';
      }else{
        let msg = res.message+":"+res.remark.message;
        console.log(msg);
        alert(msg);
      }
    }
  });
}



onMounted(()=> {
  userApi.getUserinfo().then((res)=> {
    userStore.userId = res.userId;
    getList(userStore.userId);
  });
})


</script>

<template>
  <div>
    <div v-for="item in list" class="item">
      <div class="header">
        <div class="title">
          {{item.flowTypeName}}
        </div>
        <div class="buttons">
          <van-button type="primary" size="small" @click="approve(item)">同意</van-button>
          <van-button type="danger" size="small" @click="reject(item)">退回</van-button>
        </div>
      </div>
      <div class="content">
        <div>
          编号：{{ item.flowSubject }}
        </div>
        <div>
          日期：{{ item.startDate }}
        </div>
        <div>
          申请人：{{ item.startUser }}
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.item {
  border-radius: 4px;
  border: 1px solid #ddd;
  margin: 1em;
  overflow: hidden;
  height: 10em;
  display: flex;
  flex-direction: column;
  font-size: 1em;

}

.item .header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5em;
  border-bottom: 1px solid #ddd;

}

.item .header .buttons button+button {
  margin-left: 0.5em;
}
.item .content {
  padding: 0.5em;
}
</style>